import { defineStore } from 'pinia'
import { ref } from 'vue'
export const useTagsViewStore = defineStore('tagsView', () => {
    const visitedViews = ref([])
    const cachedViews = ref([])
    function addView(view) {
        addVisitedView(view)
        addCachedView(view)
    }

    function addVisitedView(view) {
        if (visitedViews.value.some((v) => v.path === view.path)) return
        visitedViews.value.push({
            ...view,
            title: view.meta.title || 'no-name'
        })
    }

    function addCachedView(view) {
        if (cachedViews.value.includes(view.name)) return
        if (!view.meta.noCache) {
            cachedViews.value.push(view.name)
        }
    }
    //----- 标签页管理相关------------------------------------------

    function updateView(view) {
        for (let v of visitedViews.value) {
            if (v.path === view.path) {
                v = Object.assign(v, view)
                break
            }
        }
        for (let v of cachedViews.value) {
            if (!view.meta.noCache && v.path === view.path) {
                v = Object.assign(v, view)
                break
            }
        }
    }

    function delView(view) {
        return new Promise((resolve) => {
            delVisitedView(view)
            delCachedView(view)
            resolve({
                visitedViews: [...visitedViews.value],
                cachedViews: [...cachedViews.value]
            })
        })
    }
    function delVisitedView(view) {
        return new Promise((resolve) => {
            for (const [index, value] of visitedViews.value.entries()) {
                if (value.path === view.path) {
                    //移除该元素，并跳出循环
                    visitedViews.value.splice(index, 1)
                    break
                }
            }
            resolve([...visitedViews.value])
        })
    }
    function delCachedView(view) {
        return new Promise((resolve) => {
            const index = cachedViews.value.indexOf(view.name)
            if (index > -1) {
                cachedViews.value.splice(index, 1)
            }
            resolve([...cachedViews.value])
        })
    }

    function delOthersViews(view) {
        return new Promise((resolve) => {
            delOthersVisitedViews(view)
            delOthersCachedViews(view)
            resolve({
                visitedViews: [...visitedViews.value],
                cachedViews: [...cachedViews.value]
            })
        })
    }
    function delOthersVisitedViews(view) {
        return new Promise((resolve) => {
            visitedViews.value = visitedViews.value.filter((v) => {
                return v.meta.affix || v.path === view.path
            })
            resolve([...visitedViews.value])
        })
    }
    function delOthersCachedViews(view) {
        return new Promise((resolve) => {
            const index = cachedViews.value.indexOf(view.name)
            if (index > -1) {
                // 如果找到了，只保留这个视图
                cachedViews.value = [cachedViews.value[index]]
            }
            // 如果没找到，不需要更改 cachedViews.value
            // 直接返回当前的 cachedViews.value，因为其他视图不应该被删除
            resolve([...cachedViews.value])
        })
    }
    function delAllViews(view) {
        return new Promise((resolve) => {
            delAllVisitedViews()
            delAllCachedViews()
            resolve({
                visitedViews: [...visitedViews.value],
                cachedViews: [...cachedViews.value]
            })
        })
    }
    function delAllVisitedViews() {
        return new Promise((resolve) => {
            // keep affix tags
            const affixTags = visitedViews.value.filter((tag) => tag.meta.affix)
            visitedViews.value = affixTags
            resolve([...visitedViews.value])
        })
    }
    function delAllCachedViews() {
        return new Promise((resolve) => {
            cachedViews.value = []
            resolve([...cachedViews.value])
        })
    }
    function closeRightTags(view) {
        return new Promise((resolve) => {
            delRightVisitedViews(view)
            delRightCachedViews(view)
            resolve({
                visitedViews: [...visitedViews.value],
                cachedViews: [...cachedViews.value]
            })
        })
    }

    function delRightVisitedViews(view) {
        return new Promise((resolve) => {
            let _index = -1
            for (const [index, value] of visitedViews.value.entries()) {
                if (value.path === view.path) {
                    _index = index
                    break
                }
            }
            if (_index > -1) {
                for (let i = visitedViews.value.length - 1; i > _index; i--) {
                    visitedViews.value.splice(i, 1)
                }
            }
            resolve([...visitedViews.value])
        })
    }
    function delRightCachedViews(view) {
        return new Promise((resolve) => {
            const index = cachedViews.value.indexOf(view.name)
            if (index > -1) {
                for (let i = cachedViews.value.length - 1; i > index; i--) {
                    cachedViews.value.splice(i, 1)
                }
            }
            resolve([...cachedViews.value])
        })
    }
    function closeLeftTags(view) {
        return new Promise((resolve) => {
            delLeftVisitedViews(view)
            delLeftCachedViews(view)
            resolve({
                visitedViews: [...visitedViews.value],
                cachedViews: [...cachedViews.value]
            })
        })
    }
    function delLeftVisitedViews(view) {
        return new Promise((resolve) => {
            let _index = -1
            for (const [index, value] of visitedViews.value.entries()) {
                if (value.path === view.path) {
                    _index = index
                    break
                }
            }
            if (_index > -1) {
                for (let i = 0; i < _index; i++) {
                    visitedViews.value.splice(i, 1)
                }
            }
            resolve([...visitedViews.value])
        })
    }
    function delLeftCachedViews(view) {
        return new Promise((resolve) => {
            const index = cachedViews.value.indexOf(view.name)
            if (index > -1) {
                for (let i = 0; i < index; i++) {
                    cachedViews.value.splice(i, 1)
                }
            }
            resolve([...cachedViews.value])
        })
    }
    return {
        visitedViews,
        cachedViews,
        addView,
        addVisitedView,
        updateView,
        delView,
        delOthersViews,
        delCachedView,
        delAllViews,
        closeRightTags,
        closeLeftTags
    }
})
